



const _quantity ={


    changeCurrentQuantity:function (itemId) {

        console.log('check');
        $.ajax({
            type: 'GET',
            url : _common_util.getServerURL('commodity/item_detail?itemId=')+itemId,
            success:function (res) {
                let current = $("#current-quantity").val();
                if (current > res.data.stock){
                    let errorinfo ="该商品库存为："+res.data.stock+"件，已无法添加";
                    window.wxc.xcConfirm(errorinfo,'info');
                    $('#current-quantity').val(res.data.stock);
                }else {
                    let totalprice = current * res.data.price;
                    $('#product-totalprice').html(totalprice+"¥");
                }
            }
        })
    },


    add:function (itemId) {
        console.log('add')
        let current = $("#current-quantity").val();
        let prequantity = current*1 + 1*1;
        $.ajax({
            type: 'GET',
            url : _common_util.getServerURL('commodity/item_detail?itemId=')+itemId,
            success:function (res) {
                if (res.status === 0){
                    if(prequantity <= res.data.stock){
                        $('#current-quantity').val(prequantity);
                        let totalprice = prequantity * res.data.price;
                        $('#product-totalprice').html(totalprice+"¥");
                    }else {
                        let errorinfo ="该商品库存为："+res.data.stock+"件，已无法添加";
                        window.wxc.xcConfirm(errorinfo,'info');
                    }
                }
            }
        })
    },
    minus :function (itemId) {
        console.log('minus')
        let current = $("#current-quantity").val();
        let prequantity = current*1 - 1;
        if (prequantity > 0){
            $('#current-quantity').val(prequantity);
            // let totalprice = prequantity * $('#product-price').html();
            // $('#product-totalprice').html(totalprice+"¥");
            _quantity.changeCurrentQuantity(itemId);
        }
    },

    toCheckout:function (itemId) {
        console.log('check');
        $.ajax({
            type: 'GET',
            url : _common_util.getServerURL('commodity/item_detail?itemId=')+itemId,
            success:function (res) {
                let current = $("#current-quantity").val();
                if (current > res.data.stock){
                    let errorinfo ="该商品库存为："+res.data.stock+"件，没有足够库存";
                    window.wxc.xcConfirm(errorinfo,'info');
                    $('#current-quantity').val(res.data.stock);
                }else {
                    window.location.href = "single-checkout.html?itemId="+itemId+"&itemcount="+current;
                }
            }
        })
    }


}



$(function () {
    let itemId = _common_util.getURLParam('itemId');
    $.ajax({
        type: 'GET',
        url : _common_util.getServerURL('commodity/item_detail?itemId=')+itemId,
        success:function (res) {
            if (res.status === 0){
                $('#product-pic').attr('src','images/花k.png');

                $('#product-name').html(res.data.itemName);
                let purl = 'single-product.html?itemId='+res.data.itemId;

                $('#product-name').attr('href',purl);

                $('#product-price').html(res.data.price+'¥');

                $('#product-totalprice').html(res.data.price+'¥');

                $('#plus').on('click',function () {
                    _quantity.add(res.data.itemId)
                })

                $('#minus').on('click',function () {
                    _quantity.minus(res.data.itemId)
                });
                
                $('#current-quantity').change(function () {
                    _quantity.changeCurrentQuantity(res.data.itemId);
                })

                $('#checkout').on('click',function () {
                    _quantity.toCheckout(res.data.itemId);
                })

            }
        }
    })

});


